<?php

namespace App\Common\WX\Api;


use App\Common\WX\Api\msg48\Msg48;
use XWX\Common\H;
use XWX\Common\WebClient\WebClient;

/**
 * 公众号
 *
 * Class OfficialAccount
 * @package App\Common\WX\Api
 */
class OfficialAccount extends ApiBase
{
    public function msg48()
    {
        $app = new Msg48($this);

        return $app;
    }

    /**
     * 刷access_token
     *
     * @return string
     * @throws \Exception
     */
    public function funcRefreshAccessToken()
    {
        $api_url = $this->funcGetApiUrl('cgi-bin/token');

        $data = [
            'grant_type' => 'client_credential',
            'appid' => $this->config()->app_id,
            'secret' => $this->config()->app_secret,
        ];
        $string1 = H::funcStrQueryFromArray($data);

        $url = "{$api_url}?{$string1}";
        $r = $this->funcApi($url);

        $access_token = '';
        if ($r->ok())
        {
            $access_token = (string)$r->getData('access_token');
        }


        return $access_token;
    }


    /**
     * jsapi_ticket
     *
     * @param $access_token
     * @param string $type jsapi:jssdk;wx_card:卡券;
     * @return string
     * @throws \Exception
     */
    public function funcGetJsApiTicket($access_token, $type = 'jsapi')
    {
        $api_url = $this->funcGetApiUrl('cgi-bin/ticket/getticket');

        $data = [
            'type' => $type,
            'access_token' => $access_token,
        ];
        $string1 = H::funcStrQueryFromArray($data);

        $url = "{$api_url}?{$string1}";
        $r = $this->funcApi($url);

        $jsapi_ticket = '';
        if ($r->ok())
        {
            $jsapi_ticket = (string)$r->getData('ticket');
        }


        return $jsapi_ticket;
    }


    public function funcGetOauthAccessToken($code)
    {
        $api_url = $this->funcGetApiUrl('sns/oauth2/access_token');

        $data = [
            'appid' => $this->config()->app_id,
            'secret' => $this->config()->app_secret,
            'code' => $code,
            'grant_type' => 'authorization_code',
        ];
        $string1 = H::funcStrQueryFromArray($data);

        $url = "{$api_url}?{$string1}";
        $r = $this->funcApi($url);
//        if($r->ok())
//        {
//            $openid = $r->getData('openid');
//            $access_token = $r->getData('access_token');
//        }

        return $r;
    }


    public function funcGetOauthUserInfo($access_token, $openid)
    {
        $api_url = $this->funcGetApiUrl('sns/userinfo');

        $data = [
            'access_token' => $access_token,
            'openid' => $openid,
            'lang' => 'zh_CN',
        ];
        $string1 = H::funcStrQueryFromArray($data);

        $url = "{$api_url}?{$string1}";
        $r = $this->funcApi($url);


        return $r;
    }


    /**
     * 生成二维码
     * @param $word
     * @param string $type *QR_LIMIT_STR_SCENE:永久；QR_STR_SCENE:临时；
     * @return \XWX\Common\XReturn
     * @throws \Exception
     */
    public function funcQrCreate($word, $type = "QR_LIMIT_STR_SCENE")
    {
        $api_url = $this->funcGetApiUrl('cgi-bin/qrcode/create', $this->config()->access_token);

        $data = [
            'action_name' => $type,
            'action_info' => [
                "scene" => [
                    "scene_str" => $word,
                ]
            ],
        ];

        $r = $this->funcApi($api_url, WebClient::METHOD_POST, $data);


        return $r;
    }
}